package com.applet.user.service.impl;

import java.util.List;
import java.util.Objects;

import com.applet.common.constant.Constants;
import com.applet.common.core.domain.AjaxResult;
import com.applet.common.utils.DateUtils;
import com.applet.common.utils.SecurityUtils;
import com.applet.message.domain.Message;
import com.applet.message.mapper.MessageMapper;
import com.applet.user.domain.dto.ApplyForDto;
import com.applet.user.domain.vo.ApplyForVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.applet.user.mapper.ApplyForMapper;
import com.applet.user.domain.ApplyFor;
import com.applet.user.service.IApplyForService;

/**
 * 兼职申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-16
 */
@Service
public class ApplyForServiceImpl implements IApplyForService {
    @Autowired
    private ApplyForMapper applyForMapper;

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 查询兼职申请
     *
     * @param id 兼职申请主键
     * @return 兼职申请
     */
    @Override
    public ApplyFor selectApplyForById(Long id) {
        return applyForMapper.selectApplyForById(id);
    }

    /**
     * 客户端查询被兼职申请列表
     *
     * @param applyForDto 兼职申请
     * @return 兼职申请
     */
    @Override
    public List<ApplyForVo> selectClientApplyForList(ApplyForDto applyForDto) {
        ApplyFor applyFor = new ApplyFor();
        if(applyForDto.getType()==0){
            BeanUtils.copyProperties(applyForDto,applyFor);
            applyFor.setUserId(SecurityUtils.getUserId());
        }else{
            BeanUtils.copyProperties(applyForDto,applyFor);
            applyFor.setPublishUserId(SecurityUtils.getUserId());
        }
//        applyFor.setUserId(SecurityUtils.getUserId());
        applyFor.setStatus(0);
        return applyForMapper.selectApplyForList(applyFor);
    }

    /**
     * 查询兼职申请列表
     *
     * @param applyFor 兼职申请
     * @return 兼职申请
     */
    @Override
    public List<ApplyFor> selectApplyForList(ApplyFor applyFor) {
        return applyForMapper.selectApplyList(applyFor);
    }

    /**
     * 新增兼职申请
     *
     * @param applyFor 兼职申请
     * @return 结果
     */
    @Override
    public int insertApplyFor(ApplyFor applyFor) {
        Message message = new Message();
        message.setSender("管理员");
        //添加消息接收者的id
        message.setReceiveUserId(applyFor.getPublishUserId());
        message.setMessageContent("用户" + SecurityUtils.getUsername() + "申请了你的兼职，请尽快联系对方");
        //设置状态为未读
        message.setStatus(Constants.MESSAGE_UNREAD);
        message.setCreateTime(DateUtils.getNowDate());
        messageMapper.insertMessage(message);

        applyFor.setCreateTime(DateUtils.getNowDate());
        applyFor.setUserId(SecurityUtils.getUserId());
        applyFor.setNickName(SecurityUtils.getUsername());
        applyFor.setStatus(Constants.PUBLISH_STATUS_NORMAL);
        //设置状态为待办
        applyFor.setDisposeStatus(0);
        return applyForMapper.insertApplyFor(applyFor);
    }

    /**
     * 修改兼职申请
     *
     * @param applyFor 兼职申请
     * @return 结果
     */
    @Override
    public int updateApplyFor(ApplyFor applyFor) {
        applyFor.setUpdateTime(DateUtils.getNowDate());
        return applyForMapper.updateApplyFor(applyFor);
    }

    /**
     * 批量删除兼职申请
     *
     * @param ids 需要删除的兼职申请主键
     * @return 结果
     */
    @Override
    public int deleteApplyForByIds(Long[] ids) {
        return applyForMapper.deleteApplyForByIds(ids);
    }

    /**
     * 删除兼职申请信息
     *
     * @param id 兼职申请主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteApplyForById(Long id, Long publishUserId) {
        //判断当前撤销的兼职状态是否为待办，如果是待办则可以删除，否则不能删除
        if (applyForMapper.selectApplyForById(id).getDisposeStatus() != 1) {
            Message message = new Message();
            message.setSender("管理员");
            //添加消息接收者的id
            message.setReceiveUserId(publishUserId);
            message.setMessageContent("很遗憾，用户" + SecurityUtils.getUsername() + "撤销了申请");
            //设置状态为未读
            message.setStatus(Constants.MESSAGE_UNREAD);
            message.setCreateTime(DateUtils.getNowDate());
            messageMapper.insertMessage(message);
            if(applyForMapper.deleteApplyForById(id)>0){
                return AjaxResult.success("操作成功");
            }else{
                return AjaxResult.error("操作失败，请重试");
            }
        } else {
            return AjaxResult.warn("此兼职已处理，无法撤销");
        }
    }
}
